package com.atguigu.gmall.list.consumer;

import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.service.config.MqConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener
public class ListConsumer {

    @Autowired
    private ListService listService;
    //配置式
    //注解式

    //上架 接收消息
    @RabbitListener(bindings = {@QueueBinding(
            value =
            @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}
    )})
    public void onsaleMessage(
            Long skuId, Message message, Channel channel) {

        try {
            System.out.println("skuId: " + skuId);

            listService.onSale(skuId);
            //应答成功接收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //e.printStackTrace();
            //当前此消息是否为重新发送的消息
            if (message.getMessageProperties().isRedelivered()) {
                //第二次 再次投递的消息
                System.out.println("当前为第二次投递");
                try {
                    channel.basicReject(message.
                            getMessageProperties().getDeliveryTag(), false);
                    //丢弃，记录日志  人为干预
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                //是第一次 投递
                System.out.println("当前为第一次投递");
                try {
                    //否定确认
                    //requeue：是否放回队列（再给一次机会
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                            false, true);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    //下架 接收消息
    @RabbitListener(bindings = {@QueueBinding(
            value =
            @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_LOWER}
    )})
    public void cancelMessage(
            Long skuId, Message message, Channel channel) {

        try {
            System.out.println("skuId: " + skuId);

            listService.cancelSale(skuId);
            //应答成功接收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //e.printStackTrace();
            //当前此消息是否为重新发送的消息
            if (message.getMessageProperties().isRedelivered()) {
                //第二次 再次投递的消息
                System.out.println("当前为第二次投递");
                try {
                    channel.basicReject(message.
                            getMessageProperties().getDeliveryTag(), false);
                    //丢弃，记录日志  人为干预
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                //是第一次 投递
                System.out.println("当前为第一次投递");
                try {
                    //否定确认
                    //requeue：是否放回队列（再给一次机会
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                            false, true);
                } catch (Exception ex) {
                    ex.printStackTrace();

                }
            }
        }
    }
}
